package com.xianniu.ltc.model.vo;

import cn.devezhao.persist4j.engine.ID;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.core.Application;
import com.xianniu.ltc.model.entity.THuikuanjihuaDTO;
import com.xianniu.ltc.model.entity.TKaipiaoguanliDTO;
import com.xianniu.ltc.model.entity.TRbhetong0DTO;
import com.xianniu.ltc.model.entity.TRbhuikuandanDTO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: dingyx
 * @Date: 2022/3/19 4:16 下午
 */
@Slf4j
@Data
public class FinancialStatisticsVO {
    /**
     * 销售合同信息
     */
    private TRbhetong0DTO contract;
    /**
     * 销售合同回款计划
     */
    private List<THuikuanjihuaDTO> huikuanjihuaList;
    /**
     * 销售合同回款单
     */
    private List<TRbhuikuandanDTO> huikuandanList;
    /**
     * 回款计划对应的开票记录
     */
    private List<TKaipiaoguanliDTO> kaipiaoList;
    /**
     * 合同所属用户
     */
    private String owningUser = "";
    /**
     * 合同所属用户名称
     */
    private String owningUserName = "";

    /**
     * 合同所属部门
     */
    private String owningDept = "";
    /**
     * 合同所属部门名称
     */
    private String owningDeptName;
    /**
     * 客户名称
     */
    private String customerName;
    /**
     * 合同签约时间
     */
    private Date qianyueshijian;

    /**
     * 签约金额
     */
    private BigDecimal qianyuejine = BigDecimal.ZERO;
    /**
     * 合同计划回款金额
     */
    private BigDecimal jihuahuikuanjine = BigDecimal.ZERO;
    /**
     * 合同实际回款金额
     */
    private BigDecimal yihuikuanjine = BigDecimal.ZERO;
    /**
     * 合同应收金额
     */
    private BigDecimal yingshoujine = BigDecimal.ZERO;
    /**
     * 合同未收款金额（合同金额（含税）-实际已收款金额）
     */
    private BigDecimal weishoujine = BigDecimal.ZERO;
    /**
     * 合同应收未收金额
     */
    private BigDecimal yingshouweishoujine = BigDecimal.ZERO;


    /**
     * 合同已开票金额
     */
    private BigDecimal yikaipiaojine = BigDecimal.ZERO;

    /**
     * 合同已开票未回款金额
     */
    private BigDecimal yikaipiaoweihuikuanjine = BigDecimal.ZERO;

    /**
     * 构造
     * @param contract
     * @param huikuanjihuaList
     */
    public FinancialStatisticsVO(TRbhetong0DTO contract, List<THuikuanjihuaDTO> huikuanjihuaList) {
        this.contract = contract;
        this.huikuanjihuaList = huikuanjihuaList;
        this.qianyuejine = contract.getQianyuejine();
        this.qianyueshijian = contract.getHetongshengxiaoriqi();
        this.owningUser = contract.getOwningUser();
        this.owningDept = contract.getOwningDept();
        this.customerName = contract.getKehumingcheng60();
        try{
            this.owningDeptName = Application.getUserStore().getDepartment(ID.valueOf(owningDept)).getName();
        }catch (Exception e){
            log.error("部门{}不存在", owningDept);
        }
        try{
            this.owningUserName = Application.getUserStore().getUser(ID.valueOf(owningUser)).getFullName();
        }catch (Exception e){
            log.error("用户{}不存在", owningUser);
        }
        if(CollectionUtil.isNotEmpty(huikuanjihuaList)){
            this.yikaipiaojine = huikuanjihuaList.stream().map(THuikuanjihuaDTO::getKaipiaojine).reduce(BigDecimal.ZERO, BigDecimal::add);
            this.jihuahuikuanjine = huikuanjihuaList.stream().map(THuikuanjihuaDTO::getJihuashoukuanjine).reduce(BigDecimal.ZERO, BigDecimal::add);
            this.yihuikuanjine = huikuanjihuaList.stream().map(THuikuanjihuaDTO::getShijishoukuanjine).reduce(BigDecimal.ZERO, BigDecimal::add);
            // 过滤计划收款日期已过的数据
            Date now = new Date();
            log.info("当前时期是:{}", DateUtil.formatDate(now));
            for(THuikuanjihuaDTO tHuikuanjihuaDTO : huikuanjihuaList){
                if(DateUtil.compare(now, tHuikuanjihuaDTO.getYujihuikuanriqi()) > 0){
                    this.yingshoujine = NumberUtil.add(yingshoujine, tHuikuanjihuaDTO.getJihuashoukuanjine());
                    log.info("{}的预计回款日期{}已经过期", tHuikuanjihuaDTO.getHuikuanjihuaId(), tHuikuanjihuaDTO.getYujihuikuanriqi());
                }else{
                    log.info("{}的预计回款日期{}没有过期", tHuikuanjihuaDTO.getHuikuanjihuaId(), tHuikuanjihuaDTO.getYujihuikuanriqi());
                }
            }
            this.yingshouweishoujine = NumberUtil.sub(yingshoujine, yihuikuanjine);
            this.yikaipiaoweihuikuanjine = NumberUtil.sub(yikaipiaojine, yihuikuanjine);
            this.weishoujine = NumberUtil.sub(qianyuejine, yihuikuanjine);
        }
    }
}
